const router = require('koa-router')();

//模糊搜索
router.get('/api/search',async ctx => {
    let {key} = ctx.query;   //key 搜索关键词
    let sql = '';
    //1.如果没有传
    if(!key){
        sql = 'select * from product_list';
    }else{
        sql = `select * from product_list where name like '%${key}%'`;
    }
    try{
        let list = await ctx.mysql.query(sql);
        ctx.body = {
            code:1,
            data:list
        }
    }catch(e){
        ctx.body = {
            code:0,
            msg:e
        }
    }
})

module.exports = router